<?
include 'conn.php';	 

$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);

//--------获取角色的菜单权限
$sql="select * from juser where id =".$data['juser_id'];
$rs =  mysqli_query($con,$sql);
$row = $rs->fetch_assoc();
$juserMenuID=$row["menu_id"];  //1,3,4,5,7


//--------------
$result = array();
$sql="select * from menu where parent_id=-1";
$rs =  mysqli_query($con,$sql);
while($row = $rs->fetch_assoc()){

	$result_child = array();
	$result_child=has_child($row['id']);

	if($result_child){
		$row['children']=$result_child;
	}else{
		$row['children']=[];
	}

	if(contains_($juserMenuID,$row['id'])){
		array_push($result,$row);
	}
		
} 
$result_["code"] = 200;
$result_["data"] = $result;
$result_["message"] = "list";
echo json_encode($result_);
		  
function has_child($id){
	global  $con,$juserMenuID;

	$result = array();
	$sql="select * from menu where parent_id=".$id;	
	$rs_child =mysqli_query($con,$sql);	
	while($row = $rs_child->fetch_assoc()){
		
		$result_child = array();
		$result_child=has_child($row['id']);
	
		if($result_child){
			$row['children']=$result_child;
		}else{
			$row['children']=[];
		}
	
		if(contains_($juserMenuID,$row['id'])){
			array_push($result,$row);
		}
	}	
	//echo $sql."<br>";
	return $result;
} 

?>